using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Entities;
using System.Configuration;

namespace DAL
{
    public class tbChuDeDAL
    {
        private SqlConnection cn = null;
        public tbChuDeDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbChuDe chude)
        {
            SqlCommand cm = new SqlCommand("sp_tbChuDe_Insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenChuDe", chude.TenChuDe);
            cm.ExecuteNonQuery();
        }
        public void Update(tbChuDe chude)
        {
            SqlCommand cm = new SqlCommand("sp_tbChuDe_Update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaCD", chude.MaCD);
            cm.Parameters.AddWithValue("@TenChuDe", chude.TenChuDe);
            cm.ExecuteNonQuery();
        }
        public void Delete(int Id)
        {
            SqlCommand cm = new SqlCommand("sp_tbChuDe_Update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaCD", Id);
            cm.ExecuteNonQuery();
        }
        public tbChuDe.tbChuDeCollection Get(string where, int PageIndex, int PageSize, string OderBy, string OderDirection, out int TotalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_tbChuDe_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", PageIndex);
            cm.Parameters.AddWithValue("@PageSize", PageSize);
            cm.Parameters.AddWithValue("OderBy", OderBy);
            cm.Parameters.AddWithValue("@OderDirection", OderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", System.Data.DbType.Int32)).Direction = System.Data.ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbChuDe .tbChuDeCollection ChuDeColection=new tbChuDe.tbChuDeCollection ();
            while (dr.Read())
            {
                tbChuDe chude=new tbChuDe() ;
                chude.MaCD = int.Parse (dr.GetValue(0).ToString ());
                chude.TenChuDe = dr.GetValue(1).ToString();
                ChuDeColection.Add(chude);
            }
            dr.Close();
            TotalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value .ToString());
            return ChuDeColection;
        }
    }
}
